//
//  CLCollectionViewController.m
//  Charles
//
//  Created by Leo on 2017/4/6.
//  Copyright © 2017年 charles. All rights reserved.
//

#define Middle @"BBB"

#import "CLCollectionViewController.h"
#import "CLCollectionView.h"
@interface CLCollectionViewController (){

    CLCollectionView * _collectionView;
    CLCollectionView * _collectionView2;
}

@end

@implementation CLCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

- (void)setUpView {
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
    layOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    CGFloat width = self.view.bounds.size.width - 30;
    layOut.itemSize = CGSizeMake(width - 50, 200);
    layOut.minimumInteritemSpacing = 0.01f;

    _collectionView = [[CLCollectionView alloc]initWithFrame:CGRectMake(30, 100, width, 200) collectionViewLayout:layOut];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    [_collectionView setCollectionviewTag:100];
    _collectionView.decelerationRate = 0;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[CLCollectionViewCell class] forCellWithReuseIdentifier:@"collection_id"];

    UICollectionViewFlowLayout *layOut2 = [[UICollectionViewFlowLayout alloc]init];
    layOut2.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    CGFloat width2 = self.view.bounds.size.width - 30;
    layOut2.itemSize = CGSizeMake(width2 - 50, 200);
    layOut2.minimumInteritemSpacing = 0.01f;

    _collectionView2 = [[CLCollectionView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_collectionView.frame) + 30, width2, 200) collectionViewLayout:layOut2];
    [_collectionView2 setDataSource:self];
    [_collectionView2 setDelegate:self];
    [_collectionView2 setCollectionviewTag:101];
    _collectionView2.clipsToBounds = NO;
    _collectionView2.decelerationRate = 0;
    [self.view addSubview:_collectionView2];
    [_collectionView2 registerClass:[CLCollectionViewCell class] forCellWithReuseIdentifier:@"collection_id"];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection_id" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"timg.jpg"];
    cell.indexLabel.text = [NSString stringWithFormat:@"第%zi页",indexPath.row+1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"index is %@",indexPath);
}

- (CGPoint)nearestTargetOffsetForOffset:(CGPoint)offset
{
    CGFloat pageSize = self.view.bounds.size.width - 80 + 10;
    NSInteger page = roundf(offset.x / pageSize);
    CGFloat targetX = pageSize * page;
    return CGPointMake(targetX, offset.y);
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
    if (scrollView.tag == 100) {
        CGPoint targetOffset = [self nearestTargetOffsetForOffset:*targetContentOffset];
        targetContentOffset->x = targetOffset.x;
        targetContentOffset->y = targetOffset.y;
    }else {
        CGPoint originalTargetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);
        CGPoint targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(_collectionView2.bounds)/2, CGRectGetHeight(_collectionView2.bounds) / 2);
        NSIndexPath *indexPath = nil;
        NSInteger i = 0;
        while (indexPath == nil) {
            targetCenter = CGPointMake(originalTargetContentOffset.x + CGRectGetWidth(_collectionView2.bounds)/2 + 10*i, CGRectGetHeight(_collectionView2.bounds) / 2);
            indexPath = [_collectionView2.collectionView indexPathForItemAtPoint:targetCenter];
            i++;
        }
        //这里用attributes比用cell要好很多，因为cell可能因为不在屏幕范围内导致cellForItemAtIndexPath返回nil
        UICollectionViewLayoutAttributes *attributes = [_collectionView.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
        if (attributes) {
            *targetContentOffset = CGPointMake(attributes.center.x - CGRectGetWidth(_collectionView.bounds)/2, originalTargetContentOffset.y);
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation CLCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpView];
    }
    return self;
}

- (void)setUpView {
    self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    self.imageView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.imageView];
    
    self.indexLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
    self.indexLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.indexLabel];
}

@end

